<?php
/**
 * User: hufeng
 * Date: 2017/10/11
 * Desc: 权限管理
 */

namespace Admin\Controller;
use Model\NewsModel;
use Model\NewsTypeModel;

class NewsController extends BaseController{
    public $nModel;
    public $ntModel;
    public function __construct(){
        parent::__construct();
        $this->nModel = new NewsModel();
        $this->ntModel = new NewsTypeModel();
    }

    //新闻列表
    public function index(){
        if($_POST['__hash__'] || !$_GET['p']){
            session('searchParam',null);
        }
        $sessionParam = session('searchParam');

        $sessionArr['status']  = @$_POST['status'] ? $_POST['status'] : @$sessionParam['status'];
        $sessionArr['name']  = @$_POST['name'] ? $_POST['name'] : @$sessionParam['name'];
        $sessionArr['type']  = @$_POST['type'] ? $_POST['type'] : @$sessionParam['type'];
        $sessionArr['start']  = @$_POST['start'] ? $_POST['start'] : @$sessionParam['start'];
        $sessionArr['end']  = @$_POST['end'] ? $_POST['end'] : @$sessionParam['end'];
        session('searchParam',$sessionArr);
        $page = @$_GET['p'] ? $_GET['p'] : 1;

        $where = [];
        $where['status'] = array('neq',3);
        if($sessionArr['status']){
            $where['status'] = $sessionArr['status'];
        }
        if($sessionArr['name']){
            $where['title'] = array('like','%'.$sessionArr['name'].'%');
        }
        if($sessionArr['type']){
            $where['type_id'] = $sessionArr['type'];
        }
        if($sessionArr['start'] && $sessionArr['end']){
            $where['_string'] = "addtime>='{$sessionArr['start']}' and addtime<='{$sessionArr['end']} 23:59:59'";
        }
        $resList = $this->nModel->search($where,$page,$this->pernum);
        $totalNum = $this->nModel->getTotalNum($where);
        $pageShow = getPage($totalNum,$this->pernum);
        //所有分类
        $allType = $this->ntModel->allType();
        $this->assign('pageShow',$pageShow);
        $this->assign('page',$page);
        $this->assign('resList',$resList);
        $this->assign('totalNum',$totalNum);
        $this->assign('postArr',$sessionArr);
        $this->assign('allType',$allType);
        $this->display('news/index');
    }
    //新增新闻
    public function addNews(){
        if(IS_POST){
            $data = $_POST;
            $data['status'] = 2;
            $data['addtime'] = $this->dateNow;
            $id = $this->nModel->addData($data);
            $this->logModel->log('添加新闻','news',$id);
            $this->printOut(['code'=>0]);
        }
        //所有分类
        $allType = $this->ntModel->allType();
        $this->assign('allType',$allType);
        //获取分组
        $this->display('news/addnews');
    }
    //编辑新闻
    public function editNews(){
        if(IS_POST){
            $where['id'] = $_POST['id'];
            unset($_POST['id']);
            $data = $_POST;
            $this->nModel->editData($where,$data);
            $this->logModel->log('编辑新闻','news',$where['id']);
            $this->printOut(['code'=>0]);
        }

        //获取组内容
        $this->assign('page',$_GET['page']);
        $selfInfo = $this->nModel->findData(['id'=>$_GET['id']]);
        $this->assign('selfInfo',$selfInfo);
        //所有分类
        $allType = $this->ntModel->allType();
        $this->assign('allType',$allType);
        $this->display('news/editnews');
    }
    //冻结解冻新闻
    public function freezeNews(){
        $data['status'] = $_GET['status'] == 1 ? 2: 1;
        $where['id'] = $_GET['id'];
        $this->nModel->editData($where,$data);
        $this->logModel->log('冻结|解冻新闻','news',$_GET['id']);
        redirect($this->domianUrl.'/news/index?p='.$_GET['page'].'&tip=1');
    }
    //删除新闻
    public function delNews(){
        $data['status'] = 3;
        $where['id'] = $_GET['id'];
        $this->nModel->editData($where,$data);
        $this->logModel->log('删除新闻','news',$_GET['id']);
        redirect($this->domianUrl.'/news/index?p='.$_GET['page'].'&tip=1');
    }

    //新闻分类列表
    public function type(){
        if($_POST['__hash__'] || !$_GET['p']){
            session('searchParam',null);
        }
        $sessionParam = session('searchParam');

        $sessionArr['status']  = @$_POST['status'] ? $_POST['status'] : @$sessionParam['status'];
        $sessionArr['name']  = @$_POST['name'] ? $_POST['name'] : @$sessionParam['name'];
        session('searchParam',$sessionArr);
        $page = @$_GET['p'] ? $_GET['p'] : 1;

        $where = [];
        $where['status'] = array('neq',3);
        if($sessionArr['status']){
            $where['status'] = $sessionArr['status'];
        }
        if($sessionArr['name']){
            $where['name'] = array('like','%'.$sessionArr['name'].'%');
        }

        $resList = $this->ntModel->search($where,$page,$this->pernum);
        $totalNum = $this->ntModel->getTotalNum($where);
        $pageShow = getPage($totalNum,$this->pernum);
        $this->assign('pageShow',$pageShow);
        $this->assign('page',$page);
        $this->assign('resList',$resList);
        $this->assign('totalNum',$totalNum);
        $this->assign('postArr',$sessionArr);
        $this->display('news/type');
    }
    //新增新闻分类
    public function addType(){
        if(IS_POST){
            $data['name'] = $_POST['name'];
            $id = $this->ntModel->addData($data);
            $this->logModel->log('添加新闻分类','news_type',$id);
            redirect($this->domianUrl.'/news/type?tip=1');
        }
        //获取分组
        $this->display('news/addtype');
    }
    //编辑新闻分类
    public function editType(){
        if(IS_POST){
            $where['id'] = $_POST['id'];
            $data['name'] = $_POST['name'];
            $this->ntModel->editData($where,$data);
            $this->logModel->log('编辑新闻分类','news_type',$_POST['id']);
            redirect($this->domianUrl.'/news/type?p='.$_POST['page'].'&tip=1');
        }
        //获取组内容
        $this->assign('page',$_GET['page']);
        $selfInfo = $this->ntModel->findData(['id'=>$_GET['id']]);
        $this->assign('selfInfo',$selfInfo);
        $this->display('news/edittype');
    }
    //冻结解冻新闻分类
    public function freezeType(){
        $data['status'] = $_GET['status'] == 1 ? 2: 1;
        $where['id'] = $_GET['id'];
        $this->ntModel->editData($where,$data);
        $this->logModel->log('冻结|解冻新闻分类','news_type',$_GET['id']);
        redirect($this->domianUrl.'/news/type?p='.$_GET['page'].'&tip=1');
    }
    //删除新闻分类
    public function delType(){
        $data['status'] = 3;
        $where['id'] = $_GET['id'];
        $this->ntModel->editData($where,$data);
        $this->logModel->log('删除新闻分类','news_type',$_GET['id']);
        redirect($this->domianUrl.'/news/type?p='.$_GET['page'].'&tip=1');
    }

}